package pzl.com.threadrelated;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import pzl.com.threadrelated.tasks.MyThread;

public class QuitActivity extends AppCompatActivity {
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quit);

        tv = (TextView) findViewById(R.id.tv);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MyThread myThread = new MyThread();
                myThread.start();

            }
        });
    }

    public static class ThreadHandler extends Handler {

        public ThreadHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("pzl", "handleMessage start 当前线程：" + Thread.currentThread().getName());
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {

            }
            //tv.setText((CharSequence) msg.obj); //todo 错误，在非主线程设置控件
            Log.e("pzl", "handleMessage end 当前线程：" + Thread.currentThread().getName());
            getLooper().quitSafely();
        }

    }
}
